package com.jinlingxiu.app.other.db.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.jinlingxiu.app.other.db.dao.UserInfoDao
import com.jinlingxiu.app.other.db.entity.UserInfoBean


@Database(
    entities = [UserInfoBean::class],
    version = 1
)
abstract class DatabaseManager : RoomDatabase() {
    abstract fun userInfoDao(): UserInfoDao

    companion object {
        private const val DB_NAME: String = "collect.db"

        @Volatile
        private var INSTANCE: DatabaseManager? = null

        fun getInstance(context: Context): DatabaseManager =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
            }

        private fun buildDatabase(context: Context) =
            Room.databaseBuilder(
                context.applicationContext, DatabaseManager::class.java, DB_NAME
            ).allowMainThreadQueries().fallbackToDestructiveMigration()
                .build()
    }
}